import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import VueSetupExtend from "vite-plugin-vue-setup-extend";
import legacyPlugin from "@vitejs/plugin-legacy";

// https://vitejs.dev/config/
const domainName = '.china-smartech.com';
export default ({ mode }) => {
    const meta = loadEnv(mode, process.cwd());
    process.env = { ...process.env, ...meta };

    return defineConfig({
        plugins: [
            vue(),
            VueSetupExtend(), // script 增强 name 属性
            // 生成 legacy 文件 生成旧代码 build 很慢
            legacyPlugin({
                targets: ['defaults', 'ie >= 11', 'chrome 52'],  // 需要兼容的目标列表，可以设置多个
                additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
            })
        ],
        resolve: {
            alias: {
                '@': resolve(__dirname, 'src'), // 设置@指向src
            },
            extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue']// 忽略后缀
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: '@import "@/styles/settings/var.scss";@import "@/styles/tools/flex.scss";'
                },
            },
        },
        test: {
            // enable jest-like global test APIs
            globals: true,
            // simulate DOM with happy-dom
            // (requires installing happy-dom as a peer dependency)
            environment: 'happy-dom',
            // 支持tsx组件，很关键
            transformMode: {
                web: [/.[tj]sx$/]
            }
        },
        define: {
            'process.env.NODE_ENV': JSON.stringify(mode),
        },
        server: {
            host: '0.0.0.0',
            port: 5173,
            allowedHosts: [
                'zhima.local', // 允许访问的域名地址
                '.zhima.local'   // .是二级域名的通配符
            ],
            // cors: true, // 默认启用并允许任何源
            // open: true, // 在服务器启动时自动在浏览器中打开应用程序
            proxy: {
                '/v3': {
                    target: `https://${meta.VITE_URL_V3 + domainName}`, // 后端的地址
                    changeOrigin: true,
                    // rewrite: (path) => path.replace(/^\/api/, ''),
                },
                '/api': {
                    target: `https://${ meta.VITE_URL_API + domainName}`, // 后端的地址
                    changeOrigin: true,
                },
                '/restb': {
                    target: `https://${meta.VITE_URL_API + domainName}`, // 后端的地址
                    changeOrigin: true,
                },
                '/restful': {
                    target: `https://${meta.VITE_URL_RESTFUL + domainName}`, // 后端的地址
                    changeOrigin: true,
                }
            },
        },
        build: {
            chunkSizeWarningLimit: 1500,
            minify: 'terser',
            cssCodeSplit: true,// 如果设置为false，整个项目中的所有 CSS 将被提取到一个 CSS 文件中
            sourcemap: false,

            emptyOutDir: true,
            terserOptions: {
                compress: {
                    drop_console: true,// 打包时删除console
                    drop_debugger: true // 打包时删除 debugger
                },
                output: {
                    // 去掉注释内容
                    comments: true,
                }
            },
            rollupOptions: {
                output: {
                    assetFileNames: 'static/[ext]/[name].[hash].[ext]',
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id.toString().split('node_modules/')[1].split('/')[0].toString();
                        }
                    },
                }
            }
        },
    } as any)
}
